package com.groupseven.happycampus.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.MenuBo;
import com.groupseven.happycampus.mapper.CanteenMapper;
import com.groupseven.happycampus.common.pojo.LayuiPage;
import com.groupseven.happycampus.common.pojo.Menu;
import com.groupseven.happycampus.common.pojo.Restaurant_food;
import com.groupseven.happycampus.common.pojo.User;
import com.groupseven.happycampus.service.CanteenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static java.lang.Math.min;

@Service("doctorService")
public class CanteenServiceImpl implements CanteenService {

    @Autowired
    private CanteenMapper canteenMapper;


    @Override
    public LayuiPage<Menu> menuList(Map<String, String> params) {

        //解析参数
        Integer startPageNum = Integer.parseInt(params.get("page"));
        Integer size = Integer.parseInt(params.get("limit"));
        PageHelper.startPage(startPageNum, size);

        List<Menu> menus = canteenMapper.MenuList(params);


        //将查询得到的分页数据放在 PageInfo 中
        PageInfo<Menu> pageInfo = new PageInfo<>(menus);

        LayuiPage<Menu> layuiPage = new LayuiPage<>();
        //开始封装分页数据
        layuiPage.setCode(0);
        layuiPage.setMsg("分页列表数据");
        layuiPage.setCount(pageInfo.getTotal());
        layuiPage.setData(menus);

        return layuiPage;
    }

    @Override
    public LayuiPage<Menu> menuList2(Map<String, String> params) {
        //解析参数
        Integer startPageNum = Integer.parseInt(params.get("page"));
        Integer size = Integer.parseInt(params.get("limit"));
        PageHelper.startPage(startPageNum, size);

        List<Menu> menus = canteenMapper.MenuList2(params);


        //将查询得到的分页数据放在 PageInfo 中
        PageInfo<Menu> pageInfo = new PageInfo<>(menus);

        LayuiPage<Menu> layuiPage = new LayuiPage<>();
        //开始封装分页数据
        layuiPage.setCode(0);
        layuiPage.setMsg("分页列表数据");
        layuiPage.setCount(pageInfo.getTotal());
        layuiPage.setData(menus);

        return layuiPage;
    }

    @Override
    public LayuiPage<Restaurant_food> RfList(Map<String, String> params) {
        Integer startPageNum = Integer.parseInt(params.get("page"));
        Integer size = Integer.parseInt(params.get("limit"));
        PageHelper.startPage(startPageNum, size);
        //每次列表刷新都更改状态码
        rfTestList();

        List<Restaurant_food> rfs = canteenMapper.RfList(params);


        //将查询得到的分页数据放在 PageInfo 中
        PageInfo<Restaurant_food> pageInfo = new PageInfo<>(rfs);

        LayuiPage<Restaurant_food> layuiPage = new LayuiPage<>();

        //开始封装分页数据
        layuiPage.setCode(0);
        layuiPage.setMsg("分页列表数据");
        layuiPage.setCount(pageInfo.getTotal());
        layuiPage.setData(rfs);

        return layuiPage;
    }

    @Override
    public int rfadd(Restaurant_food rf) {
        return canteenMapper.rfadd(rf);
    }

    @Override
    public List<Restaurant_food> rfTestList() {
        List<Restaurant_food> rfs = canteenMapper.rfTestList();
        for (Restaurant_food rf :
                rfs) {
            if (rf.getCabbage() < 5 || rf.getCarrot() < 5 || rf.getMeat() < 5 || rf.getTomato() < 5) {
                canteenMapper.rfupdate("警告！无法出餐！", rf.getId());
            } else if (rf.getCabbage() < 100 || rf.getCarrot() < 100 || rf.getMeat() < 100 || rf.getTomato() < 100) {
                canteenMapper.rfupdate("严重！食材紧缺！", rf.getId());
            }
            if (rf.getCabbage() > 100 || rf.getCarrot() > 100 || rf.getMeat() > 100 || rf.getTomato() >100 ) {
                canteenMapper.rfupdate("正常", rf.getId());
            }

        }
        return rfs;
    }

    @Override
    public List<Restaurant_food> rfTestList(Integer foodId) {
        List<Restaurant_food> rfs = canteenMapper.rfTestList();

        canteenMapper.rfupdate("警告！无法出餐！", foodId);

        return rfs;
    }

    @Override
    public User ProvePayUser(Integer id, String paypassword) {

        return canteenMapper.ProvePayUse(id, paypassword);
    }


    @Override
    public int Pay(Integer userId, Integer[] menuId, Integer foodId, Integer out) {

        int[] i = new int[menuId.length];
        int [] pay =new int[menuId.length];
        int a = 100;
        try{//开启事务
        for (int j = 0; j < menuId.length; j++) {
            i[j] = canteenMapper.addFoodUser(userId, menuId[j], foodId, out);
            int foodUserId = canteenMapper.getFoodUserId();
            pay[j] = canteenMapper.Pay(foodUserId, out);
        }
        Arrays.sort(i);
        Arrays.sort(pay);

        }catch (Error error){
            return 0;
        }finally {
            return min(i[0], pay[0]);
        }
    }


    @Override
    public int getallprice(Integer[] menuId, Integer out) {
        int getallprice = 0;

        for (int i = 0; i < menuId.length; i++) {
            getallprice = getallprice + canteenMapper.getallprice(menuId[i]) + out;
        }
        return 0;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int removeById(int id) {
        return canteenMapper.removeById(id);
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int updateMenu(Menu menu) {
        return canteenMapper.updateMenu(menu);

    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int addMenu(Menu menu) {

        return canteenMapper.addMenu(menu);
    }

    @Override
    public int deleteAll(String idstr) {

        return canteenMapper.deleteAll(idstr);
    }

    @Override
    public int addMenus(List<Menu> list) {
        return canteenMapper.addMenus(list);
    }

    @Override
    public List<MenuBo> MenuExcelData(Menu menu) {
        return canteenMapper.MenuExcelData(menu);
    }


}
